<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/15
 * Time: 15:08
 */

namespace Admin\Model;
use Common\Model\UserModel as BaseFUserModel;

class MoneyModel extends BaseModel{

    protected $trueTableName = 't_money';

    /**
     * where条件设置
     * @param   $condition
     * @param   $tbName
     * @return  $where
     */
    static function setCondition($condition, $tbName = ''){
        $tbName     = $tbName ? $tbName.'.' : '';
        $where['_string']  = '1 = 1';

        //商品类型查询
        if(isset($condition['goods_type']) && is_numeric($condition['goods_type'])){
            $where[$tbName.'goods_type']    = ['eq', $condition['goods_type']];
            //商品ID查询
            if(isset($condition['goods_id']) && is_numeric($condition['goods_id'])){
                $where[$tbName.'goods_id']  = ['eq', $condition['goods_id']];
            }
        }

        //时间查询
        if(isset($condition['time_start']) && is_numeric($condition['time_start'])){
            $where[$tbName.'create_time'][]   = ['egt', $condition['time_start']];
        }
        if(isset($condition['time_end']) && is_numeric($condition['time_end'])){
            $where[$tbName.'create_time'][]   = ['lt', $condition['time_end']];
        }

        if(isset($condition['status'])){
            $where[$tbName.'status']    = $condition['status'];
        }
        
        return $where;
    }

    /**
     * 字段处理
     * @param   $data
     * @param   $batch
     * @return  $data
     */
    static function resolveColumn($data, $batch = false){
        $goodsType  = [1 => '购买课程', 2 => '问答', 3 => '用户提现', 4 => '购物车', 5 => '会员购买'];
        $roleText   = BaseFUserModel::ROLE_MAP;
        $resolveColumn   = function(&$item) use ($goodsType, $roleText){
            $item['create_date']    = date('Y-m-d', $item['create_time']);
            $item['goods_type_text']    = $goodsType[$item['goods_type']];
            $item['role_text']  = $roleText[$item['role_id']];
            $item['profit'] = $item['amount'] - $item['deduct'];
            if($item['goods_type'] == 5){
                $vipInfo    = C('VIP_TYPE.'.$item['goods_id']);
                $item['goods_name']  = $vipInfo['date_desc'];
                $item['price']  = $vipInfo['amount'];
            }
        };

        if($batch){
            foreach($data as &$item){
                $resolveColumn($item);
            }
        }else{
            $resolveColumn($data);
        }

        return $data;
    }

    /**
     * 详情交易信息
     * @author  王中艺
     * @param   $moneyId    订单ID
     * @return  $moneyInfo
     */
    function detailInfo($moneyId){
        $moneyInfo  = $this->table('t_money a')
            ->join('left join t_coupon_code b on a.code_id = b.id')
            ->join('left join t_user c on a.user_id = c.id')
            ->where('a.id = %d', $moneyId)
            ->field('a.id, a.user_id, c.nickname, a.amount, a.deduct, a.create_time, a.status,
                a.trade_no, b.code, b.amount code_amount')
            ->cache(true)
            ->find();
        $moneyInfo['create_date']   = date('Y-m-d', $moneyInfo['create_time']);
        $moneyInfo['profit']    = $moneyInfo['amount'] - $moneyInfo['deduct'];
        return $moneyInfo;
    }

    /**
     * 获取交易列表
     * @param   $condition
     * @param   $page
     * @param   &$pageHtml
     * @param   $total  总计信息查询
     * @return  $list
     */
    function getList($condition, $page, &$pageHtml, &$total){
        $where  = self::setCondition($condition, 'a');
        //关键词查询
        if(isset($condition['keyword']) && $condition['keyword'] != ''){
            $where['_string']   = sprintf('(b.id = "%d" or b.nickname like "%%%s%%")', $condition['keyword'], $condition['keyword']);
        }

        $query  = $this->table('t_money a')
            ->join('left join t_user b on a.user_id = b.id')
            ->where($where)
            ->cache(true);

        //分页设置
        if($pageHtml === true){
            $Page   = new \Think\Page((clone $query)->count(), C('PAGE_COUNT'));
            $pageHtml   = $Page->angularShow();
        }

        //总计信息查询
        if($total === true){
            $total  = (clone $query)->field('count(*) count, sum(a.amount) amount, sum(deduct) deduct')->find();
            $total['profit']    = $total['amount'] - $total['deduct'];
        }

        $list   = $query
            ->page($page, C('PAGE_COUNT'))
            ->field('a.*, b.nickname, b.truename, b.role_id')
            ->select();

        $list   = self::resolveColumn($list, true);

        return $list;
    }

    /**
     * 导出交易列表
     * @param   $goodsType
     * @param   $start
     * @param   $length
     * @param   $timeS
     * @param   $timeE
     * @return  count or list
     */
    function outExcel($goodsType, $start, $length, $timeS, $timeE){
        $where  = 'a.status = 1';
        if($goodsType)
            $where  .= ' and a.goods_type = '.$goodsType;
        if($timeS)
            $where  .= ' and a.create_time >= '.$timeS;
        if($timeE)
            $where  .= ' and a.create_time <= '.$timeE;

        
        if(is_numeric($start)){
            $list   = M('t_money as a', null)
                ->join('left join t_user as b on a.user_id = b.id')
                ->where($where)
                ->limit($start, $length)
                ->field('a.id, a.user_id, a.trade_no, a.amount, a.deduct, a.create_time, a.status,
                    a.goods_type, a.goods_id, b.nickname, b.truename, b.role_id')
                ->cache(true)
                ->select();

            return $this->_analyField('deduct', $list, true);
        }else{
            $count  = M('t_money as a', null)
                ->where($where)
                ->cache(true)
                ->count();

            return $count;
        }
    }
}